package com.hdyg.sixg.activity.home;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.entry.CashBean;
import com.hdyg.sixg.mvp.Contrant.CCash;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PCash;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 提现界面
 * @time 2019/4/8 10:31
 */
public class CashActivity extends BaseActivity implements CCash.IVCash {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.sv_balance)
    SuperTextView svBalance;
    @BindView(R.id.sv_bank_card)
    SuperTextView svBankCard;
    @BindView(R.id.et_cash_number)
    EditText etCashNumber;
    @BindView(R.id.sv_6g)
    SuperTextView sv6g;
    @BindView(R.id.sv_fee)
    SuperTextView svFee;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.tv_fee_des)
    TextView tvFeeDes;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private CCash.IPCash mPresenter;
    private String amount, payPass;
    private CashBean dataBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cash;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.cash_title);
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        etCashNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                amount = s.toString();
                //6g链=money/price
                //手续费=6g链*free
                if (!TextUtils.isEmpty(amount)) {
                    double str1 = Double.valueOf(amount) / Double.valueOf(dataBean.getPrice());
                    double str2 = Double.valueOf(amount) / Double.valueOf(dataBean.getPrice()) * Double.valueOf(dataBean.getFree())/100;
                    sv6g.setRightString(StringUtil.dealFloatStr(str1));
                    svFee.setRightString(StringUtil.dealFloatStr(str2));
                }
            }
        });
        llTopLeft.setOnClickListener(v -> finish());

        btSure.setOnClickListener(v -> {
            amount = etCashNumber.getText().toString().trim();
            payPass = etPayPwd.getText().toString().trim();
            if (TextUtils.isEmpty(amount)) {
                toastNotifyShort(R.string.cash_number_hint);
                return;
            }
            if (TextUtils.isEmpty(payPass)) {
                toastNotifyShort(R.string.pay_pwd_hint);
                return;
            }
            if (StringUtil.isFastDoubleClick()){
                return;
            }
            mPresenter.pGetApplyCash(BaseUrl.APPLY_WITHDRAW_URL, GetParamUtil.getApplyCashParam(amount, payPass));
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.WITHDRAW_URL, GetParamUtil.getCashParam());
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PCash(this);
    }

    @Override
    public void vGetInitSuccess(CashBean dataBean) {
        this.dataBean = dataBean;
        svBalance.setRightString(dataBean.getCare_wa());
        svBankCard.setLeftBottomString(dataBean.getBankcard());
        String feeStr = String.format(getResources().getString(R.string.cash_fee_des), dataBean.getFree());
        tvFeeDes.setText(feeStr);
    }

    @Override
    public void vGetApplyCashSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        setResult(RESULT_OK);
        finish();
    }
}
